#include "snake.hh"
Snake::Snake()
{
	index = 0;
	prevPart = nullptr;	
	nextPart = NULL;
	
	type = Head;
	direction = Right;
	position.x=0;
	position.y=0;
	position.w=64;
	position.h=64;

	permittedX = new int[70];
	permittedY = new int[70];
	for (int i = 0; i <70 ; ++i) {
		*(permittedX+i) = -1;
		*(permittedY+i) = -1;
	}



		
}

Snake::Snake(Snake* prevPart, int index)
{
	this->index = index;
	this->prevPart = prevPart;
	this->permittedX = prevPart->permittedX;
	this->permittedY = prevPart->permittedY;
	nextPart = NULL;
	type = Tail;
	direction = prevPart->getDirection();
	prevDirection = direction;
	position = *(prevPart->getPrevPosition());
	prevPosition = position;
	
}

SDL_Rect* Snake::getPosition()
{
	return &position;
}

SDL_Rect* Snake::getPrevPosition()
{
	return &prevPosition;
}


void Snake::setDirection(const int direction)
{
	this->direction = direction;
}

const int Snake::getDirection()
{
	return direction;
}
const int Snake::getPrevDirection()
{
	return prevDirection;
}

void Snake::update()
{

	if(nextPart != NULL && type != Head)
		type = Middle;

	if(prevPart != NULL)
		direction = prevPart->getPrevDirection();
	
	prevPosition = position;
	
	if(direction == Right)
	{
		position.x += 64;
	}else if(direction == Left)
	{
		position.x -= 64;
	}
	else if(direction == Down)
	{
		position.y += 64;
	}else if(direction == Up)
	{
		position.y -= 64;
	}

	if(nextPart != NULL)
	{
		nextPart->update();
	}

	prevDirection = direction;

	*(permittedX+index) = position.x;
	*(permittedY+index) = position.y;





}
int Snake::getSpriteRectIndex()
{
	if(type == Head)
	{
		if(direction == Right)
			return 0;
		else if(direction == Left)
		       return 1;
		else if(direction == Up)
			return 2;
		else if(direction == Down)
			return 3;	
	}

	if(type == Middle)
	{
		if(direction == prevPart->getDirection())
		{
		if(direction == Right || direction == Left)
		       	return 4;
		else if(direction == Up || direction == Down)
			return 5;
		}
		else
		{
			if((direction == Right && prevPart->getDirection() == Down) || (direction == Up && prevPart->getDirection() == Left))
				return 6;
			else if((direction == Right && prevPart->getDirection() == Up) || (direction == Down && prevPart->getDirection() == Left))
				return 7;
			else if((direction == Left && prevPart->getDirection() == Down) || (direction == Up && prevPart->getDirection() == Right))
				return 8;	
			else if((direction == Left && prevPart->getDirection() == Up) || (direction == Down && prevPart->getDirection() == Right))
				return 9;	

		}
	}

	if(type == Tail)
	{
		if(direction == prevPart->getDirection())
		{
		if(direction == Right)
			return 10;
		else if(direction == Left)
		       return 11;
		else if(direction == Up)
			return 12;
		else if(direction == Down)
			return 13;
		}
		else
		{

	if((direction == Right && prevPart->getDirection() == Down) || (direction == Up && prevPart->getDirection() == Left))
				return 6;
			else if((direction == Right && prevPart->getDirection() == Up) || (direction == Down && prevPart->getDirection() == Left))
				return 7;
			else if((direction == Left && prevPart->getDirection() == Down) || (direction == Up && prevPart->getDirection() == Right))
				return 8;	
			else if((direction == Left && prevPart->getDirection() == Up) || (direction == Down && prevPart->getDirection() == Right))
				return 9;	
		}	
	}
	return 1;
}

void Snake::draw(SDL_Renderer* renderer, SDL_Texture* texture, Sprites* sprites)
{
	SDL_RenderCopy(renderer, texture, sprites->getSprite(getSpriteRectIndex()), getPosition());
	if(nextPart != NULL)
	{
		nextPart->draw(renderer, texture, sprites);
	}
}

void Snake::score(int index)
{
	if(type == Tail)
		type = Middle;
	if(nextPart != NULL)
		nextPart->score(index);
	else
		nextPart = new Snake(this, index);
}


